/**
 *	\file AKCPPTTTPlayer.cpp
 *  
 *  \section desc_sec Description
 *  Description
 *  
 *  \if copyright
 *  	Copyright 2008 Ben Chen. All rights reserved.
 *  \endif
 *  
 *  \section hist_sec Change History:
 *  	-	1/22/08		BC		Created this file
 *  
 *  <hr>
 *  \addtogroup AKCPP
 *  @{
 */

#include "AKCommon.h"
#include "AKCPPTTTPlayer.h"

AKCPPTTTPlayer::AKCPPTTTPlayer(AKSideIndex aSide, unsigned int aNumOfSides)
: PARENT(aSide, aNumOfSides)
{
	lastMove = AKZeroMove;
}

AKCPPTTTPlayer::~AKCPPTTTPlayer()
{
	;
}

//	State
void AKCPPTTTPlayer::backToInitialState()
{
	for (int i = 0; i < NUM_CELLS; ++i)
	{
		cells[i].cell = i;
		cells[i].piece = NSNotFound;
		cells[i].side = NSNotFound;
	}
	
	lastMove = AKZeroMove;
}

bool AKCPPTTTPlayer::performMove(const AKMove newMove)
{
	bool retVal = false;
	
	if (cells[newMove.dstCell].piece == NSNotFound)
	{
		cells[newMove.dstCell].piece = newMove.piece;
		cells[newMove.dstCell].side  = newMove.side;
		
		retVal = true;
	}
	
	return retVal;
}

bool AKCPPTTTPlayer::canUndo()
{
	bool retVal;
	
	if (lastMove.piece != NSNotFound && lastMove.dstCell != NSNotFound)
		retVal = true;
	else
		retVal = false;
	
	return retVal;
}

bool AKCPPTTTPlayer::undo()
{
	bool retVal;
	
	if (canUndo())
	{
		// Only one time of undo is supported here.
		cells[lastMove.dstCell] = AKZeroPiece;
		lastMove = AKZeroMove;
	}
	else
		retVal = false;
	
	return retVal;
}

//	Player
const AKMove AKCPPTTTPlayer::getBestMove()
{
	AKMove bestMove;
	bestMove.piece		= NSNotFound;
	bestMove.side		= NSNotFound;
	bestMove.srcCell	= NSNotFound;
	bestMove.dstCell	= NSNotFound;
	
	for (int i = 0; i < NUM_CELLS; ++i)
	{
		if (cells[i].piece == NSNotFound)
		{
			bestMove.piece		= 0;			// There is only one kind of piece.
			bestMove.side		= PARENT::side;
			bestMove.srcCell	= NSNotFound;	// Since the piece is newly created.
			bestMove.dstCell	= i;
			
			break;
		}
	}
	
	return bestMove;
}

const AKMove AKCPPTTTPlayer::getAlternativeMove()
{
	// Do nothing;
	return AKZeroMove;
}

/** @} */
